<template>
  <el-card>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="openModel(null)"
      style="margin-bottom: 8px;">新增</el-button>
    <el-table :data="topicList" style="width: 100%" border>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="答案：">
              <span>{{ props.row.answer }}</span>
            </el-form-item>
            <el-form-item label="示例：">
              <span>{{ props.row.example }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号" type="index">
      </el-table-column>
      <el-table-column width="180" label="题目类别" prop="categoryName">
      </el-table-column>
      <el-table-column label="题目名称" prop="content">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openModel(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="delAnswer(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px; text-align: center" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="queryInfo.current"
      :page-sizes="[8, 10, 15, 20]" :page-size="queryInfo.size"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog :visible.sync="dialogVisible" width="30%">
      <div class="dialog-title-container" slot="title" ref="voteInfoTitle"></div>
      <el-form :model="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="题目类别">
          <el-select v-model="ruleForm.categoryId" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目名称" size="mini">
          <el-input v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item label="题目答案">
          <el-input type="textarea" v-model="ruleForm.answer"></el-input>
        </el-form-item>
        <el-form-item label="示例" size="mini">
          <el-input v-model="ruleForm.example"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEdit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>

</template>

<script>
import {
  getTopicList,
  addTopicList,
  editTopicList,
  deleteTopicList,
} from '@/api';
export default {
  data() {
    return {
      topicList: [],
      queryInfo: {
        size: 8,
        current: 1,
      },
      total: 0,
      dialogVisible: false,
      ruleForm: {
        answer: '',
        content: '',
        id: null,
        categoryId: 3,
        example: '',
      },
      options: [
        {
          id: 3,
          name: '社会现象',
        },
        {
          id: 4,
          name: '态度观点',
        },
        {
          id: 5,
          name: '组织管理',
        },
        {
          id: 6,
          name: '应急应变',
        },
        {
          id: 7,
          name: '人际关系',
        },
        {
          id: 8,
          name: '情景模拟',
        },
        {
          id: 9,
          name: '复合化题型',
        },
        {
          id: 10,
          name: '演讲',
        },
        {
          id: 11,
          name: '其他题型',
        },
      ],
      // rules: {
      //   content: [
      //     { required: true, message: '请输入题目名称', trigger: 'blur' },
      //   ],
      //   answer: [
      //     { required: true, message: '请输入对应答案', trigger: 'blur' },
      //   ],
      //   example: [{ required: true, message: '请输入示例', trigger: 'blur' }],
      // },
    };
  },
  mounted() {
    this.listTopic();
  },
  methods: {
    listTopic() {
      getTopicList(this.queryInfo).then(({ data }) => {
        console.log(data);
        this.topicList = data.data.records;
        this.total = data.data.total;
      });
    },
    handleSizeChange(val) {
      this.queryInfo.size = val;
      this.listTopic();
    },
    handleCurrentChange(val) {
      this.queryInfo.current = val;
      this.listTopic();
    },
    openModel(row) {
      if (row != null) {
        this.$refs.voteInfoTitle.innerHTML = '编辑';
        this.ruleForm = JSON.parse(JSON.stringify(row));
      } else {
        this.$refs.voteInfoTitle.innerHTML = '新增';
        this.ruleForm = {
          answer: '',
          content: '',
          id: null,
          categoryId: 3,
          example: '',
        };
      }
      this.dialogVisible = true;
    },
    addOrEdit() {
      if (this.ruleForm.id != null) {
        editTopicList(this.ruleForm).then((res) => {
          console.log(res);
          if (res.status == 200) {
            this.listTopic(this.queryInfo);
            this.$message({
              type: 'success',
              message: '修改成功!',
            });
          } else {
            this.$message.error('修改失败');
          }
          this.dialogVisible = false;
        });
      } else {
        addTopicList(this.ruleForm).then((res) => {
          console.log(res);
          if (res.status == 200) {
            this.listTopic(this.queryInfo);
            this.$message({
              type: 'success',
              message: '添加成功!',
            });
          } else {
            this.$message.error('添加失败');
          }
          this.dialogVisible = false;
        });
      }
    },
    delAnswer(id) {
      // let idList = [];
      // idList = { data: [id] };
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          deleteTopicList(id).then((res) => {
            if (res.status == 200) {
              this.listTopic(this.queryInfo);
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
            } else {
              this.$message.error('删除失败');
            }
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
  },
};
</script>

<style>
</style>